<template>
  <div>
    <!-- 面包屑组件 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/editpost' }"
        >文章列表</el-breadcrumb-item
      >
    </el-breadcrumb>
    <!-- 列表组件 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="user.nickname" label="作者" width="180">
      </el-table-column>
      <el-table-column label="缩略图" width="144">
        <template slot-scope="scope">
          <img
            v-if="scope.row.cover.length > 0"
            :src="fixUrl(scope.row.cover[0].url)"
            alt=""
            class="avatarImg"
          />
          <img v-else src="../assets/134.jpg" alt="" class="avatarImg" />
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="200">
        <template slot-scope="scope">
          {{ scope.row.create_date.split("T")[0] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="$router.push('/editpost?id=' + scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[2, 4, 6, 10]"
      :page-size="pageSize"
      @current-change="currentChange"
      @size-change="sizeChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageIndex: 1,
      pageSize: 4,
    };
  },
  created() {
    this.loadPage();
  },
  methods: {
    fixUrl(oldUrl) {
      const regExp = /^http/;
      if (regExp.test(oldUrl)) {
        return oldUrl;
      } else {
        return this.$axios.defaults.baseURL + oldUrl;
      }
    },
    loadPage() {
      this.$axios({
        url: "/post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        console.log(res.data);
        this.tableData = res.data.data;
        this.total = res.data.total;
      });
    },
    currentChange(newIndex) {
      this.pageIndex = newIndex;
      this.loadPage();
    },
    sizeChange(sizeIndex) {
      this.pageSize = sizeIndex;
      this.loadPage();
    },
  },
};
</script>

<style lang="less" scoped>
.el-table {
  font-size: 20px;
  color: #000;
}
.avatarImg {
  width: 100%;
  height: 80px;
  object-fit: cover;
  border-radius: 15px;
}
/deep/.el-pagination {
  margin-top: 50px;
  .el-pager li {
    font-size: 16px;
  }
}
</style>